package edu.zjut.hjg.saas.biz.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import edu.zjut.hjg.saas.biz.dao.ActivityDao;
import edu.zjut.hjg.saas.biz.model.Activity;
import edu.zjut.hjg.saas.biz.support.Paginated;

public class ActivityDaoImpl extends SqlMapClientDaoSupport implements ActivityDao {

    @Override
    public Integer create(Activity activity) {
        return (Integer) getSqlMapClientTemplate().insert("activity.create", activity);
    }

    @Override
    public void deleteByIdAndAppId(int id, int appId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("appId", appId);
        getSqlMapClientTemplate().delete("activity.deleteByIdAndAppId", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Activity> listByAlbumIdAndAppId(int albumId, int appId, int pageSize, int page) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("albumId", albumId);
        params.put("appId", appId);
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject(
                "activity.countByAlbumIdAndAppId", params);
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Activity> list = getSqlMapClientTemplate().queryForList(
                "activity.listByAlbumIdAndAppId", params);
        Paginated<Activity> paginated = new Paginated<Activity>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }

    @Override
    public Activity load(Integer id) {
        return (Activity) getSqlMapClientTemplate().queryForObject("activity.load", id);
    }

    @Override
    public Activity findByIdAndAppId(int id, int appId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("appId", appId);
        return (Activity) getSqlMapClientTemplate().queryForObject("activity.findByIdAndAppId",
                params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Activity> listAllByAlbumIdAndAppId(int albumId, int appId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("albumId", albumId);
        params.put("appId", appId);
        return getSqlMapClientTemplate().queryForList("activity.listAllByAlbumIdAndAppId", params);
    }

    @Override
    public void deleteByAlbumIdAndAppId(int albumId, int appId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("albumId", albumId);
        params.put("appId", appId);
        getSqlMapClientTemplate().delete("activity.deleteByAlbumIdAndAppId", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Activity> listFirstsGroupByAlbumIds(List<Integer> albumIds) {
        return getSqlMapClientTemplate().queryForList("activity.listFirstsGroupByAlbumIds",
                albumIds);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Activity> listByLimitAndAppId(int limit, int appId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("limit", limit);
        params.put("appId", appId);
        return getSqlMapClientTemplate().queryForList("activity.listByLimitAndAppId", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Activity> listByLimit(int limit) {
        return getSqlMapClientTemplate().queryForList("activity.listByLimit", limit);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Paginated<Activity> listAll(int pageSize, int page) {
        Integer totalSize = (Integer) getSqlMapClientTemplate().queryForObject("activity.countAll");
        int start = pageSize * (page - 1);
        if (start > totalSize) {
            start = (totalSize / pageSize) * pageSize;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageSize", pageSize);
        params.put("start", start);
        List<Activity> list = getSqlMapClientTemplate().queryForList("activity.listAll", params);
        Paginated<Activity> paginated = new Paginated<Activity>();
        paginated.setValues(list);
        paginated.setPageSize(pageSize);
        paginated.setStart(start);
        paginated.setTotalSize(totalSize);
        return paginated;
    }
}
